package com.jf.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-10-24
 * Time:15:05
 */
@Order(1)
@Aspect
@Component
public class CalculateAspect {
    @Pointcut(value = "execution(* *.*(..))")
    public void pointCut(){}

    @Before(value = "execution(public double com.jf.spring.aop.SmartCal.add(double, double))")
    public void beginLog(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        Object target = joinPoint.getTarget();
        System.out.println("方法执行前--日志--切面目标对象-" + target + "--方法名-" + signature.getName() + "--参数-" + Arrays.toString(args));
    }
    @AfterReturning(value = "execution(public double com.jf.spring.aop.SmartCal.add(..))", returning = "ret")
    public void endLog(JoinPoint joinPoint, Object ret) {
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("方法执行后--日志--方法名-" + signature.getName() + "--参数-" + Arrays.toString(args) + "--返回值-" + ret);
    }
    @After(value = "execution(* com.jf.spring.aop.SmartCal.*(..))")
    public void finallyLog(JoinPoint joinPoint) {
        Object target = joinPoint.getTarget();
        System.out.println("方法最终执行后--日志--切面目标对象-" + target);
    }
    @AfterThrowing(value = "pointCut()", throwing = "thrown")
    public void exceptionLog(JoinPoint joinPoint, Throwable thrown) {
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        Object target = joinPoint.getTarget();
        System.out.println("方法抛出异常时--日志--切面目标对象-" + target + "--方法名-" + signature.getName() + "--参数-" + Arrays.toString(args)
        + "--异常-" + thrown);
    }
    @Before(value = "execution(public void A.sayHi())")
    public void ABefore(JoinPoint joinPoint) {
        System.out.println("A before");
    }
}
